package com.fsc.fragment;

import java.util.List;

import com.fsc.fragment.outcome.OutCome;
import com.fsc.fragment.outcome.OutComeDao;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Fragment implements OnClickListener{
	private ListView lV_cost;
	private OutComeAdapter adapter;
	private List<OutCome> data;
	private OutComeDao Dao;
	private int position;
	private View v;
	
	// 底部菜单4个Button
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	
	//ll_background换背景图片
	private LinearLayout ll_background;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.activity_home,container, false);
		
		lV_cost = (ListView) v.findViewById(R.id.lV_cost);
		
		//给下标4个按钮初始化
		btn1 = (Button) v.findViewById(R.id.btn1);
		btn2 = (Button) v.findViewById(R.id.btn2);
		btn3 = (Button) v.findViewById(R.id.btn3);
		btn4 = (Button) v.findViewById(R.id.btn4);
		
		ll_background = (LinearLayout) v.findViewById(R.id.ll_background);
		initEvent();
		adapter = new OutComeAdapter();
		Dao = new OutComeDao(getActivity());

		//查询
		data = Dao.queryTop();

		adapter.notifyDataSetChanged();
		//给listview
		lV_cost.setAdapter(adapter);
		return v;
	}
	private void initEvent() {
		// 设置按钮监听
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);         
    }

	@Override
	public void onResume() {
		super.onResume();
		data.clear();
		data = Dao.queryTop();
		adapter.notifyDataSetChanged();
		lV_cost.setAdapter(adapter);
	}
	//创建适配器
	class OutComeAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHodler hodler=null;
			if(convertView == null){//判断缓存对象是否为null,不为空时已经缓存了对象
				hodler=new ViewHodler();
				//convertView = View.inflate(getActivity(),R.layout.item_inaccount, null);
				convertView=View.inflate(getActivity(),R.layout.item_outcome, null);
				hodler.tv_money=(TextView)convertView.findViewById(R.id.tv_money);
				//hodler.tv_type = (TextView)convertView.findViewById(R.id.sp_time);
				hodler.tv_address=(TextView)convertView.findViewById(R.id.tv_address);
				hodler.tv_time=(TextView)convertView.findViewById(R.id.tv_time);
				convertView.setTag(hodler);
				
			}else{//等于null说明第一次显示，则TextView都是新创建的
				hodler=(ViewHodler)convertView.getTag();
			}
			OutCome outCome = data.get(position);

			
			hodler.tv_money.setText(""+outCome.getMoney());;
			hodler.tv_address.setText(outCome.getAddress());
			hodler.tv_time.setText(outCome.getTime());
			return convertView;
		}
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if(requestCode == 1 && resultCode == 1){
			OutCome outCome = new OutCome();
			//1、设置数据
			outCome.setMoney(Double.parseDouble(intent.getStringExtra("money")));
			outCome.setTime(intent.getStringExtra("time"));
			outCome.setType(intent.getStringExtra("type"));
			outCome.setAddress(intent.getStringExtra("address"));
			outCome.setMark(intent.getStringExtra("mark"));
			//2、执行添加操作
			Dao.insert(outCome);
			//3、添加到list中
			data.add(0,outCome);
			//4、通知list更新
			adapter.notifyDataSetChanged();
			Toast.makeText(getActivity(), "新增收入成功！", Toast.LENGTH_LONG).show();
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn1:
			//1、加载当前的fragment
			btn1.setBackgroundResource(R.drawable.im2);
			ll_background.setBackgroundResource(R.drawable.bg4);
			btn2.setBackgroundResource(R.drawable.im1);
			btn3.setBackgroundResource(R.drawable.im1);
			btn4.setBackgroundResource(R.drawable.im1);
			break;
		case R.id.btn2:
			btn1.setBackgroundResource(R.drawable.im1);
			btn2.setBackgroundResource(R.drawable.im2);
			ll_background.setBackgroundResource(R.drawable.bg2);
			btn3.setBackgroundResource(R.drawable.im1);
			btn4.setBackgroundResource(R.drawable.im1);
			break;
		case R.id.btn3:
			btn1.setBackgroundResource(R.drawable.im1);
			btn2.setBackgroundResource(R.drawable.im1);
			btn3.setBackgroundResource(R.drawable.im2);
			ll_background.setBackgroundResource(R.drawable.bg3);
			btn4.setBackgroundResource(R.drawable.im1);
			break;
		case R.id.btn4:
			btn1.setBackgroundResource(R.drawable.im1);
			btn2.setBackgroundResource(R.drawable.im1);
			btn3.setBackgroundResource(R.drawable.im1);
			btn4.setBackgroundResource(R.drawable.im2);
			ll_background.setBackgroundResource(R.drawable.bg1);
			break;
		default:
			break;
		}
	}
}
